home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / 1430K_FR / BIG_FORM.C < prev   
C/C++ Source or Header  |  1992-03-04  |  11KB  |  490 lines

  1. /* Inside Mac Vol.IV p.240 */
  2. #if 0
  3. typedef struct HFSDefaults{
  4.     char sigWord[2];        /* signature word, default 'BD' */
  5.     long abSize;            /* allocation block size in bytes, defaut 0 
  6.                             meaning (1+(VolSize in blocks/64K)  *512 bytes */
  7.     long clpSize;            /* clump size in bytes, default 4*abSize */
  8.     long nxFreeFN;            /* next free file number, default 16 */
  9.     long btClpSize;            /* B*-tree clump size in bytes, default 0
  10.                             meaning (volsize in blocks)/128*512 bytes */
  11.     short rsrv1,rsrv2,rsrv3;    /* reserved */
  12.     } HFSDefaults;
  13. #endif
  14.  
  15. se si dê annulla, non espelle il disco
  16.  
  17.  
  18. /* bisognerebbe stampare i valori correnti, se Å un disco Mac !!!!!!!! */
  19.  
  20. /*    challenges for disk hackers:
  21. create unusual formats by talking directly to the hardware disk interface 
  22. use sectors 0&1 as extensions tree
  23. make the last two sectors of the disk available for files
  24. create a reduced Desktop file before the Finder creates it, so that the maximum 
  25.     size is reached even under System 6
  26. create a disk where (for example) sector 0 says "I'm an MS-DOS disk, but sectors 2 to 
  27.     719 are occupied by a foreign partition", and sector 2 says "I'm a Mac disk, but 
  28.     sectors 720 to 1439 are bad and can't be used"
  29. reduce the size allocated to extension tree, catalog & desktop without losing
  30.     the files currently in the disk
  31. */
  32.  
  33. mcopy(dest, src, len)
  34. register char*dest,*src;
  35. register int len;
  36. {
  37.      while (--len>=0) *dest++ = *src++;
  38. }
  39.  
  40. fillmem(dest, ch, len)
  41. register char*dest;
  42. register char ch;
  43. register int len;
  44. {
  45.     while (--len>=0) *dest++ = ch;
  46. }
  47.  
  48. #define NULL 0L
  49.  
  50. char buffer[512];
  51. short drive_number;
  52. short err_code;
  53. static Point aPoint={80,52};
  54. Cursor    waitCursor;
  55.  
  56.  
  57. #define d_quit 1
  58. #define d_clump 2
  59. #define d_extents 3
  60. #define d_check 6
  61. #define d_diskinsertion -1
  62.  
  63. /* Pascal string -> integer */
  64. int pstrtoi(str)
  65. Str255 str;
  66. {
  67. register int i=str[0];
  68. register unsigned char *p=&str[1];
  69. register unsigned int val=0;
  70.  
  71. while(i>0 && *p==' ') p++,i--;
  72. while(i>0 && *p>='0' && *p <='9' ){
  73.     val = val*10+ *p -'0';
  74.     p++;    i--;
  75.     }
  76. return val;
  77. }
  78.  
  79.  
  80. Boolean is_not_initialized()
  81. {
  82. /* There is not a device driver call for this purpose, but that range of
  83. errors is typical for non-initialized disks */
  84. leggi_settore(0,buffer);
  85. if(err_code<=-66 && err_code>=-71){
  86.     leggi_settore(2,buffer); 
  87.     if(err_code<=-66 && err_code>=-71) return true;
  88.     }
  89. return false;
  90. }
  91.  
  92.  
  93. main()
  94. {
  95. short        kind;
  96. Handle        h;
  97. Rect        r;
  98. short itemHit;
  99. DialogPtr myDialog;
  100. HFSDefaults my_defs;
  101. EventRecord myEvent;
  102. Boolean riguarda_me;
  103. Boolean force_init;
  104. short j;
  105.  
  106. InitMacintosh();
  107. h = (Handle)GetCursor(watchCursor);
  108. waitCursor = **(CursHandle)h;
  109.  
  110. myDialog = GetNewDialog (128,NULL,(char*)-1);
  111.  
  112. for(;;){
  113.     ShowWindow(myDialog);
  114.     /* handle the dialog */
  115.     itemHit=0;
  116.     do{
  117.         SystemTask();
  118.         riguarda_me = GetNextEvent(everyEvent, &myEvent);
  119.         if(riguarda_me && myEvent.what==diskEvt){
  120.             itemHit=d_diskinsertion;
  121.             drive_number=(short)(myEvent.message);
  122.             }
  123.         else if( IsDialogEvent(&myEvent)){
  124.             if(DialogSelect(&myEvent,&myDialog,&j))
  125.                 if(j==d_check){
  126.                     GetDItem(myDialog,d_check,&kind,&h,&r);
  127.                     SetCtlValue((ControlHandle)h,!GetCtlValue(h));
  128.                     }
  129.                 else
  130.                     itemHit=j;
  131.             }
  132.         }
  133.     while (itemHit != d_diskinsertion && itemHit != d_quit );
  134.     if(itemHit==d_quit) ExitToShell();
  135.  
  136.     HideWindow(myDialog);
  137.  
  138.     GetDItem(myDialog,d_clump,&kind,&h,&r);
  139.     GetIText(h,buffer);
  140.     my_defs.clpSize=(long)pstrtoi(buffer)<<9;
  141.  
  142.     GetDItem(myDialog,d_extents,&kind,&h,&r);
  143.     GetIText(h,buffer);
  144.     my_defs.btClpSize=(long)pstrtoi(buffer)<<9;
  145.  
  146.     GetDItem(myDialog,d_check,&kind,&h,&r);
  147.     force_init=!GetCtlValue(h);
  148.  
  149.     /*DisposDialog(myDialog);*/
  150.  
  151.     my_defs.sigWord[0]='B'; my_defs.sigWord[1]='D';
  152.     my_defs.abSize=512;
  153.     my_defs.nxFreeFN=1;
  154.     my_defs.rsrv1=my_defs.rsrv2=my_defs.rsrv3=0;
  155.  
  156.     FmtDefaults=&my_defs;
  157.  
  158.     if( force_init || is_not_initialized() || *(int*) buffer== 0x4244 && ((*(int*)&buffer[10])&0x200))
  159.             /* System 7 sets that flag if one or more bad sectors were found and
  160.             marked as unusable */
  161.         DIBadMount(aPoint, myEvent.message);
  162.     else
  163.         mac_ize_disk((short)myEvent.message);
  164.  
  165.     FmtDefaults=NULL;
  166.     }
  167. }
  168.  
  169. static Handle icon_handle;
  170. static Rect iconRect={10,16,42,48};
  171.  
  172.  
  173. static pascal Boolean icon_update (theDialog, theEvent,itemHit)
  174. /* update routine for the disk initialization dialog: we've tried to
  175. copy the standard disk initialization dialog used by DIBadMount
  176. */
  177. DialogPtr theDialog;
  178. EventRecord *theEvent;
  179. int *itemHit;
  180. {
  181. if(theEvent->what==updateEvt && (WindowPtr)theEvent->message==theDialog){
  182.     GrafPtr    savePort;
  183.     short    kind;
  184.     Handle    h;
  185.  
  186.     GetPort( &savePort );
  187.     SetPort(theDialog);
  188.     PlotIcon(&iconRect,icon_handle);
  189.     SetPort(savePort);
  190.     }
  191. return false;
  192. }
  193.  
  194.  
  195. mac_ize_disk(drive_number)
  196. short drive_number;
  197. {
  198. /* see Technical Notes 70 and 272 */
  199. cntrlParam       ParamBlock;
  200. DialogPtr initDialog;
  201. short item;
  202. short    kind;
  203. Handle    h;
  204. Rect    r;
  205. char*volname;
  206.  
  207. ParamBlock.ioRefNum=-5;                  /*.SONY driver*/
  208. ParamBlock.ioVRefNum=drive_number;
  209. ParamBlock.csCode=21;                    /* Return Physical Drive Icon */
  210. icon_handle=NewHandle(128);    
  211. if(PBControl(&ParamBlock,0) != noErr)
  212.     fillmem(*icon_handle,0,128);
  213. else
  214.     mcopy(*icon_handle, *((Ptr*)&ParamBlock.csParam[0]), 128);
  215.  
  216. initDialog=GetNewDialog(132,NULL,-1L);
  217.  
  218. leggi_settore(2,buffer);
  219. if( *(int*) buffer== 0xD2D7 || *(int*) buffer== 0x4244) 
  220.     volname=&buffer[36];
  221. else
  222.     volname="\pUntitled";
  223. GetDItem(initDialog,5,&kind,&h,&r);
  224. SetIText(h,volname);
  225. ModalDialog(icon_update,&item);
  226.  
  227. GetIText(h,&buffer);
  228.  
  229. if(item!=2){
  230.     return;
  231.     }
  232. else{
  233.     int     *IPtr;
  234.     GrafPtr    savePort;
  235.     UnmountVol(NULL,drive_number);
  236.     GetPort( &savePort );    
  237.     SetPort(initDialog);
  238.     for(item=1;item<=5;item++){
  239.         GetDItem(initDialog,item,&kind,&h,&r);
  240.         InsetRect(&r,-4,-4);
  241.         EraseRect(&r);
  242.         }
  243.     SetCursor(&waitCursor);
  244.     MoveTo(64,20);
  245.     DrawString("\pCreating directory...");
  246.     DIZero (drive_number,buffer);
  247.     SetPort(savePort);
  248.     SetCursor(&arrow);
  249.     }
  250. DisposDialog(initDialog);
  251. DisposHandle(icon_handle);
  252. }
  253.  
  254.  
  255.  
  256. #if 0
  257. main()
  258. {
  259. HFSDefaults my_defs;
  260. EventRecord        myEvent;
  261. int riguarda_me,catalog_sect;
  262. static Point aPoint={80,112};
  263.  
  264. static unsigned char desktop[]="\pDesktop",
  265.     finder[]="\pFinder 1.0";
  266.  
  267. printf("\f");
  268. my_defs.sigWord[0]='B'; my_defs.sigWord[1]='D';
  269. my_defs.abSize=512;
  270. my_defs.clpSize=1024;
  271. my_defs.nxFreeFN=1;
  272. my_defs.btClpSize=1024;
  273. my_defs.rsrv1=my_defs.rsrv2=my_defs.rsrv3=0;
  274.  
  275. do{
  276.     riguarda_me = GetNextEvent(everyEvent, &myEvent);
  277.     }
  278. while(!riguarda_me||myEvent.what!=diskEvt);
  279.  
  280. printf("vado!\n");
  281.  
  282. FmtDefaults=&my_defs;
  283.  
  284. DIBadMount(aPoint, myEvent.message);
  285.  
  286. FmtDefaults=NULL;
  287. return;
  288.  
  289. {int io,refnum;
  290. Handle h;
  291. long count;
  292. static IOParam pb;
  293. int drive_number =1;
  294. io=SetVol(NULL,drive_number);
  295. CreateResFile(desktop);
  296. refnum=OpenResFile(desktop);
  297.  
  298. h=NewHandle( (Size) sizeof(finder) );
  299. mcopy(*h, finder, sizeof(finder) );
  300. AddResource(h,'STR ',0,"\p");
  301. /*UpdateResFile(refnum);*/
  302.  
  303. CloseResFile(refnum);
  304.  
  305.  
  306.  
  307. {
  308. FileParam fpb;
  309. fpb.ioFVersNum = 0;
  310. fpb.ioFDirIndex = 0;
  311. fpb.ioVRefNum=drive_number;
  312. fpb.ioNamePtr=desktop;
  313. io=PBGetFInfo(&fpb, 0);
  314. /*fpb.ioFlFndrInfo.fdFlags |= fInvisible;*/
  315. fpb.ioFlFndrInfo.fdType='FNDR';
  316. fpb.ioFlFndrInfo.fdCreator='ERIK';
  317. io=PBSetFInfo(&fpb, 0);
  318. }
  319.  
  320. pb.ioVersNum = 0;
  321. pb.ioPermssn = fsWrPerm;
  322. pb.ioMisc = 0;
  323. pb.ioNamePtr=desktop;
  324. pb.ioVRefNum=drive_number;
  325.  
  326. io=PBOpenRF(&pb, 0);
  327.  
  328. count=1536;
  329. io=Allocate (refnum,&count);
  330. printf("Allocate=%d\n",io);
  331. FSClose(refnum);
  332.  
  333. UnmountVol (NULL,drive_number);
  334. Eject(NULL,drive_number);
  335.  
  336. }
  337. return;
  338.  
  339. {int io,refnum;
  340. long count;
  341. static IOParam pb;
  342.  
  343. io=Create(desktop, 1, 'ERIK', 'FNDR');
  344. printf("create=%d\n",io);
  345.  
  346. io = FSOpen( desktop, 1, &refnum );
  347. printf("open=%d\n",io);
  348. count=1536;
  349. io=Allocate (refnum,&count);
  350. printf("Allocate=%d\n",io);
  351.  
  352. pb.ioVersNum = 0;
  353. pb.ioPermssn = fsWrPerm;
  354. pb.ioMisc = 0;
  355. pb.ioNamePtr=(unsigned char*)desktop;
  356. pb.ioVRefNum=1;
  357.  
  358. io=PBOpenRF(&pb, 0);
  359. printf("openRF=%d\n",io);
  360. count=sizeof(desktop);
  361. printf("size=%ld\n",count);
  362. io=FSWrite( refnum, &count, desktop );
  363. printf("write=%d\n",io);
  364. }
  365.  
  366.  
  367. UnmountVol (NULL,1);
  368. Eject(NULL,1);
  369. return;
  370.  
  371. leggi_settore(2,buffer);
  372. catalog_sect= *((int*)&buffer[150]) +4+1;
  373. printf("cat=%d\n",catalog_sect);
  374. leggi_settore(catalog_sect,buffer);
  375.  
  376. {int i=0;
  377. int ok=0;
  378. int desktop_start,old_desk_size;
  379. static char desk_name[]="\pDesktop";
  380.  
  381. for(i=0;i<512 && ok<sizeof(desk_name);i++){
  382.     if(buffer[i]==desk_name[ok])
  383.         ok++;
  384.     else
  385.         ok=0;
  386.     }
  387. printf("i=%d\n",i);
  388. if(buffer[i]!=2){
  389.     printf("non ci siamo");
  390.     return;
  391.     }
  392. #define new_size 2
  393. /* attento: che ci siano problemi di allineamento? */
  394. desktop_start= *((int*)&buffer[i+86]) +4;
  395. old_desk_size= *((int*)&buffer[i+88]);
  396. printf("desk start=%d\n",desktop_start);
  397. *((long*)&buffer[i+40])=(long)(new_size*512);    /* physical length */
  398. *((int*)&buffer[i+88])=new_size;        /* size of extent */
  399. *((int*)&buffer[i+72])=2048;            /* clump size */
  400. scrivi_settore(catalog_sect,buffer);    /* il catalogo ora dice che il desktop Å corto */
  401. leggi_settore(3,buffer);
  402.  
  403. for(i=desktop_start+new_size-4;i<desktop_start+old_desk_size-4;i++)
  404.     BitClr(buffer,(long)i);
  405. scrivi_settore(3,buffer);        /* la mappa bit ora dice che i settori sono liberi */
  406.  
  407. leggi_settore(2,buffer);
  408. *((int*)&buffer[34]) += old_desk_size-new_size;    /* unused allocation blocks */
  409. scrivi_settore(2,buffer);        /* registra anche questo */
  410. scrivi_settore(*((int*)&buffer[18])+4,buffer);    /* e la copia lass¥, quel campo
  411.         dê il numero di settori nel disco - i primi 4 - quegli ultimi 2 */
  412.  
  413. }
  414.  
  415.  
  416. }
  417. #endif
  418.  
  419.  
  420. InitMacintosh()
  421. {
  422.     InitGraf(&thePort);
  423.     InitFonts();
  424.     FlushEvents( everyEvent, 0 );
  425.     InitWindows();
  426.     InitMenus();
  427.     TEInit();
  428.     InitDialogs(0L);
  429.     InitCursor();
  430.     MaxApplZone();
  431. }
  432.  
  433.  
  434. leggi_settore(sect_n,buffer)
  435. int sect_n;
  436. char *buffer;
  437. {
  438. /* from Inside Macintosh page II/216 */
  439. #define _Read 0xA002
  440. asm{
  441.     moveq    #24,D0             /* #<ioQElsize/2>-1,D0 */
  442. clrloop:    
  443.     clr.w    -(SP)
  444.     dbra D0,@clrloop
  445.     move.l    SP,A0
  446.     move.w    #-5,0x18(A0)    /* #-5,ioRefNum(A0) */
  447.     move.w    drive_number,0x16(A0)    /* #1,ioDrvNum(A0) internal drive */
  448.     move.w    #1,0x2C(A0)        /* #1,ioPosMode(A0) absolute positioning */
  449.     clr.l    D0
  450.     move.w    sect_n,D0
  451.     mulu    #512,D0
  452.     move.l    D0,0x2E(A0)        /* D0,ioPosOffset(A0) */
  453.     move.l    #512,0x24(A0)    /* #512,ioReqCount(A0)    /* read one sector */
  454.     move.l    buffer,A1
  455.     move.l    A1,0x20(A0)        /* A1,ioBuffer(A0) */
  456.     dc.w _Read
  457.     move.w    0x10(A0),err_code    /* ioResult(A0),err_code * /
  458.     add.w    #0x32,SP        /* #ioQElSize,SP */
  459.     }
  460. }
  461.  
  462.  
  463.  
  464. scrivi_settore(sect_n,buffer)
  465. int sect_n;
  466. char *buffer;
  467. {
  468. #define _Write 0xA003
  469. asm{
  470.     moveq    #24,D0
  471. clrloop:    
  472.     clr.w    -(SP)
  473.     dbra D0,@clrloop
  474.     move.l    SP,A0
  475.     move.w    #-5,0x18(A0)
  476.     move.w    drive_number,0x16(A0)
  477.     move.w    #1,0x2C(A0)
  478.     move.w    sect_n,D0
  479.     mulu    #512,D0
  480.     move.l    D0,0x2E(A0)
  481.     move.l    #512,0x24(A0)
  482.     move.l    buffer,A1
  483.     move.l    A1,0x20(A0)
  484.     dc.w _Write
  485.     move.w    0x10(A0),err_code
  486.     add.w    #0x32,SP
  487.     }
  488. }
  489.  
  490.